package com.redlight.base;

import com.alibaba.fastjson.annotation.JSONField;
import org.apache.commons.lang3.StringUtils;
import com.redlight.utils.ApplicationContextProvider;
import com.redlight.jdbc.Column;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by chao on 2019/1/9.
 */
public  abstract class BaseEntity extends BaseBean{
    @Column("id")
    protected String id;
    protected String createBy;
    @JSONField(
            format = "yyyy-MM-dd HH:mm:ss"
    )
    protected Date createDate;
    protected String updateBy;
    @JSONField(
            format = "yyyy-MM-dd HH:mm:ss"
    )
    protected Date updateDate;
    @Column("enable")
    protected String enable;
    protected String delFlag;
    protected String delBy;
    protected String remark;
    @JSONField(
            serialize = false
    )
    protected boolean isNewRecord;
    private String dsf;
    private String whereClause;
    private String orderBy;
    private Page page;

    public BaseEntity() {
        this.isNewRecord = false;
        this.dsf = "";
        this.whereClause = "";
        this.orderBy = "";
        this.page = new Page();
    }

    public BaseEntity(String id) {
        this();
        this.id = id;
    }

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCreateBy() {
        return this.createBy;
    }

    public void setCreateBy(String createBy) {
        this.createBy = createBy;
    }

    public String getUpdateBy() {
        return this.updateBy;
    }

    public void setUpdateBy(String updateBy) {
        this.updateBy = updateBy;
    }

    public String getEnable() {
        return this.enable;
    }

    public void setEnable(String enable) {
        this.enable = enable;
    }

    public String getRemark() {
        return this.remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getDelFlag() {
        return this.delFlag;
    }

    public void setDelFlag(String delFlag) {
        this.delFlag = delFlag;
    }

    public Date getCreateDate() {
        return this.createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public Date getUpdateDate() {
        return this.updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    public String getDelBy() {
        return this.delBy;
    }

    public void setDelBy(String delBy) {
        this.delBy = delBy;
    }

    @JSONField(
            serialize = false
    )
    public String getDsf() {
        return this.dsf;
    }

    public void setDsf(String dsf) {
        this.dsf = dsf;
    }

    @JSONField(
            serialize = false
    )
    public String getWhereClause() {
        return this.whereClause;
    }

    public void setWhereClause(String whereClause) {
        this.whereClause = whereClause;
    }

    @JSONField(
            serialize = false
    )
    public String getOrderBy() {
        return this.orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    @JSONField(
            serialize = false
    )
    public Page getPage() {
        return this.page;
    }

    public void setPage(Page page) {
        this.page = page;
    }

    public boolean getIsNewRecord() {
        this.isNewRecord = this.isNewRecord || StringUtils.isBlank(this.getId());
        return this.isNewRecord;
    }

    public void setIsNewRecord(boolean isNewRecord) {
        this.isNewRecord = isNewRecord;
    }

    public void preSave() {
        SysUser user = ApplicationContextProvider.getUser();
        if(this.getIsNewRecord()) {
            if(StringUtils.isBlank(this.id)) {
                this.setId(IdGen.uuid());
            }

            if(null != user) {
                this.setCreateBy(user.getId());
            }

            this.setIsNewRecord(true);
            this.setCreateDate(new Date());
            this.setDelFlag("0");
        } else {
            this.setUpdateDate(new Date());
            if(null != user) {
                this.setUpdateBy(user.getId());
            }
        }

    }

    @JSONField(
            serialize = false
    )
    public static HashMap newParamMap() {
        HashMap m = new HashMap();
        m.put("delFlag", "0");
        m.put("dbName", Global.getDbType());
        m.put("dsf", "");
        return m;
    }
}
